/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.ISysUserConnector;
import com.itttq.gshop.generic.util.Constants;

@Auth(excls={"login"})
@RestController
@RequestMapping("sys/user")
public class SysUserController extends GenericController {
	private static final long serialVersionUID = 87980154043862210L;
	@Autowired
	private ISysUserConnector sysUserConnector;
	
	@RequestMapping(value = "login.html", method = RequestMethod.POST)
	public ModelAndView login(String username, String password, HttpServletRequest request) {
		ModelAndView mv= new ModelAndView("login");
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			mv.addObject("msg", "用户名或密码不能为空！");
			return mv;
		}
		SysUserPo user = sysUserConnector.login(username, password);
		// 放入session
		if(null == user) {
			mv.addObject("msg", "用户名或密码错误！");
		} else {
			this.setSession(request, Constants.SESSION_USER, user);
			// 判断是否有上一次登录记录，有就调回到登录之前页面
			Object obj = this.getSession(request, Constants.SESSION_URL_LAST);
			mv.setViewName("redirect:" + (null == obj ? "/main.html" : obj.toString()));
		}
		return mv;
	}
	
	/**
	 * 根据条件查询SysUserPo对象，并分页
	 * 
	 * @param sysUser
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("users.html")
	public ModelAndView getSysUsers(SysUserPo sysUser, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/sys/users");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		sysUser.setStart(ppt.getStart());
		sysUser.setSize(ppt.getSize());
		ResultPageTo<SysUserPo> rpt = sysUserConnector.getSysUsers(sysUser);
		this.getPageMv("users", mv, rpt);
		mv.addObject("user", sysUser);
		return mv;
	}

	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add.html")
	public ModelAndView addSysUser() {
		ModelAndView mv = new ModelAndView("/sys/userAdd");
		
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}.html")
	public ModelAndView viewSysUser(@PathVariable("id") Integer id) {
		ModelAndView mv = addSysUser();
		if(id != null) {
			mv.addObject("user", sysUserConnector.getSysUser(id));
		}
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}.html")
	public ModelAndView editSysUser(@PathVariable("id") Integer id) {
		ModelAndView mv = viewSysUser(id);
		mv.addObject("op", "edit");
		return mv;
	}
	
	/**
	 * 保存SysUserPo对象，无ID则新增，有ID则更新
	 * 
	 * @param sysUser
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@Auth
	@RequestMapping("save.html")
	public ModelAndView saveSysUser(SysUserPo sysUser, HttpServletRequest request) {
		sysUser.setRegisterIp(Constants.getIp(request));
		sysUserConnector.saveSysUser(sysUser);
		return getSysUsers(new SysUserPo(), null);
	}

	/**
	 * 根据ID删除SysUserPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@Auth
	@RequestMapping("del/{id}.html")
	public ModelAndView delSysUser(@PathVariable("id") Integer id) {
		if(id != null) {
			sysUserConnector.delSysUser(id);
		}
		return getSysUsers(new SysUserPo(), null);
	}

	/**
	 * 根据ID批量删除SysUserPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@Auth
	@RequestMapping("delBatch.html")
	public ModelAndView delSysUsers(String ids) {
		if(StringUtils.isNotBlank(ids)) {
			System.out.println("删除数据为："+ids);
		}
		return getSysUsers(new SysUserPo(), null);
	}
}